/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.KhachHangDAO;
import com.miracleteam.miracle.entities.Khachhang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;



/**
 *
 * @author DangKhoa
 */
public class KhachHangDAOImpl implements KhachHangDAO{
    Logger logger= Logger.getLogger(TinhThanhDAOImpl.class);
    public Khachhang LayKhachHang(String ma) {
        Khachhang kh = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            //tt = (Tinhthanh)session.get(Tinhthanh.class, ma);
            String hql = "From Khachhang where DaXoa=0 AND maKhachHang=:maKhachHang";
            Query query = session.createQuery(hql);
            query.setString("maKhachHang", ma);
            Object ob = query.uniqueResult();
            kh =  (Khachhang)ob;
        } catch (HibernateException e) {
             logger.error(e.getMessage());
        } finally {
            session.close();
        }
        return kh;
    }

    public Khachhang KiemTraDangNhap(String tenDangNhap, String MatKhau) {
        Khachhang kh = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
           
            String hql = "From Khachhang where DaXoa=0 AND tenTruyCap=:tenDN AND matKhau=:matKhau";
            Query query = session.createQuery(hql);
            query.setString("tenDN", tenDangNhap);
            query.setString("matKhau",MatKhau);
            Object ob = query.uniqueResult();
            kh =  (Khachhang)ob;
        } catch (HibernateException e) {
             logger.error(e.getMessage());
        } finally {
            session.close();
        }
        return kh;
    }
    public String PhatSinhMaKhachHang()
    {
        String kq="ND-";
        Date date = new Date();
        
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }
}
